{template header}

<div class="container">

<div class="panel panel-default">
  <div class="panel-body">
<div class="row">
  <div class="col-md-2"></div>
  <div class="col-md-8">
  
<form method="POST" action="{SITE_URL}index.php?app=gift&ac=add&ts=do"  enctype="multipart/form-data" id="form1" >
  <div class="form-group">
    <label>名称：</label>
    <input name="title" id="title" type="text" class="form-control">
  </div>

  <div class="form-group">
    <label>属性：</label>
    <p>
        <input type="radio" name="cate" checked value="1" onclick="showdanjia()">义卖物品
        <input type="radio" name="cate" value="0" onclick="showjifen()">志愿者福利

    </p>
  </div>
  
  {if $arrType}
  <div class="form-group">
    <label>类型：</label>

<select class="form-control" name="typeid">
{loop $arrType $key $item}
<option value="{$item[typeid]}">{$item[typename]}</option>
{/loop}
</select>
  </div>
  {/if}
  

  <div class="form-group">
    <label>到期时间(最多5天，超期后未售出仅卖家可见)：</label>
    <p><input class="form-control" name="limittime" id="limitTime" readonly value="" /> </p>
  </div>

  <div class="form-group">
    <label>数量：</label>
    <p><input class="form-control" id="quota" name="quota" value="" /> </p>
  </div>


  <div class="form-group">
        <label id="label1" style="display:none">所需积分：</label><label id="label2">单价：</label>
        <p><input class="form-control" id="price" name="price" /> </p>
 </div>


  <div class="form-group">
    <label>发货地：</label>
    <div id="city_3">
      <p><select id="province" name="province" class="prov form-control"></select></p>
      <p><select id="city" name="city" class="city form-control" disabled="disabled"></select></p>
      <p><input type="text" id="address" name="address" class="form-control" value=""></p>
    </div>
  </div>
  
  <div class="form-group">
    <label>交货方式：</label>
    <p>
      <select name="transfer" id="transfer"  class="form-control">
          <option value="2">包邮</option>
        <option value="1">自取</option>

      </select>
    </p>
  </div>

  
  <div class="form-group">
    <label>封面：</label>
    <div class="">
       <img id="picshow" valign="middle" src="" style="height: 150px;">
       <div style="display: flex;">
         <input type="file" id="picfile" name="picfile">
         <div id="picsubmit" class="btn btn-success">上传照片</button> 
         <input type="hidden" name="photo" id="photo" />
       </div>
    </div>
  </div>
  
  
  <div class="form-group">
    <label>简介{if $draft==''}<a class="btn btn-xs" href="{php echo(curPageURL())}&draft=1">恢复丢失内容</a>{/if}</label>
    <div id="tseditor"></div>
    <div style="display: none;">
      <textarea id="content" name="content"></textarea>
    </div>
    <!--<textarea name="content" id="tseditor"></textarea>-->
  </div>

<div class="form-group">
  <input class="btn btn-success" type="submit" value="发布">
</div>

  
</form>
  
  
  </div>
  <div class="col-md-2"></div>
</div>

</div>
</div>
</div>

<div id="protocol" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true"  style="overflow:scroll;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h5 class="modal-title">发起义卖须知</h5>
            </div>
            <div class="modal-body" >

                <div class="form-group">
                    {$strTopic['content']}
                </div>

                <div class="form-group">
                    <center><a class="btn btn-default" onclick="closeit()" >我知道了</a></center>
                </div>

            </div>
        </div>
    </div>
</div>

<script>
    $('#protocol').modal('show');
</script>


<script src="{SITE_URL}public/js/image.js"></script>
<script>
    $(function(e){
        $('#form1').on('submit', function(e) {
            var title  = $('#title').val();
            var limittime  = $('#limitTime').val();
            var price = $('#price').val();
            var quota  = $('#quota').val();
            var content  = $('#content').val();
            var photo = $('#photo').val();


            if(title==''){
                bootbox.alert('名称必填');
                return false;
            }

            if(limittime==''){
                bootbox.alert('到期时间必填');
                return false;
            }

            if(quota==''){
                bootbox.alert('数量必填');
                return false;
            }

            if(price==''){
                bootbox.alert('价格必填');
                return false;
            }
            if(photo==''){
                bootbox.alert('必须上传封面');
                return false;
            }
            if (content == '<p><br></p>') {
                bootbox.alert('内容不能为空');
                return false;
            }


        });
        
        var picFile = null;
        // 封面上传监听
        $('#picfile').on("change", function (e) {
          $("#photo").val('')
          $("#picshow").attr('src', ''); 

          let file = e.target.files[0]
          compressImg(file, 0.25).then(res => {
            $("#picshow").attr('src', window.URL.createObjectURL(res.file)); 
            picFile = res.file
          })
        });
        
        var upload_url = "{SITE_URL}index.php?app=gift&ac=do&ts=photo";
        $('#picsubmit').on("click", function () {
          if (!picFile) {
            return bootbox.alert('请选择图片！');
          }
          var formData = new FormData();
          formData.append("file", picFile, picFile.filename);
          formData.append("upload_file", true);
          console.log(formData, picFile)
          $.ajax({
            url: upload_url,
            data: formData,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function(data){
              if (data != 0) {
                $("#photo").val(data)
                picFile = null
                  return bootbox.alert('图片上传成功！');
              } else {
                  return bootbox.alert('图片上传失败！');
              }
            }
          });
        });

    });



 function showjifen(){
   $('#label2').css('display','none');
   $('#label1').css('display','block');
     $('#protocol').modal('hide');
 }

 function showdanjia(){
   $('#label1').css('display','none');
   $('#label2').css('display','block');
     $('#protocol').modal('show');
 }

 function GetDateStr(AddDayCount) {
     var dd = new Date();
     dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
     var y = dd.getFullYear();
     var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期，不足10补0
     var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号，不足10补0
     return y+"-"+m+"-"+d;
 }

 $('#limitTime').datetimepicker({
    language:  'zh-CN',
    autoclose: true,
    endDate: GetDateStr(5),
    format: 'yyyy-mm-dd hh:ii'
  });


 function closeit(){
     $('#protocol').modal('hide');
 }
</script>
{php doAction('tseditor')}
{template footer}